package com.mobiletrain.org.motherkitch.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mobiletrain.org.motherkitch.MainActivity;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.SplashActivity;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/4/25.
 */
public class SplashAdapter extends PagerAdapter {
    private Activity context;
    private ArrayList<View> views;

    public SplashAdapter(Activity context, ArrayList<View> views) {
        this.context = context;
        this.views = views;
    }

    @Override
    public int getCount() {
        if (views != null) {
            return views.size();
        }
        return 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        if (position == views.size() - 1) {
            View v = views.get(position);
            ImageView imageView = (ImageView) v.findViewById(R.id.sp_gohome);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent();
                    i.setClass(context, MainActivity.class);

                    SharedPreferences sharedPreferences = context.
                            getSharedPreferences(SplashActivity.SHARED_NAME,
                                    Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putBoolean(SplashActivity.SHARED_NAME, false);
                    edit.commit();

                    context.startActivity(i);
                    context.finish();
                }
            });
        }
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
